package com.hnas.trade.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.util.StringUtil;
import com.hnas.trade.dao.AbnormalOrderDao;
import com.hnas.trade.dao.TradeRecordDao;
import com.hnas.trade.dto.DTOTradeRecord;
import com.hnas.trade.dto.intf.DTOSearchGetComsumDiscount;
import com.hnas.trade.orm.AbnormalOrder;
import com.hnas.trade.orm.TradeRecord;

@Component("tradeRecordService")
//@Transactional(isolation=Isolation.READ_COMMITTED,readOnly=false,propagation=Propagation.REQUIRED,value="transactionManagerHnt")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class TradeRecordServiceImpl implements TradeRecordService{
	
	@Autowired
	private TradeRecordDao tradeRecordDao;
	
	@Autowired
	private AbnormalOrderDao abnormalOrderDao;

	@Override
	public TradeRecord saveOrUpdate(DTOTradeRecord dto) throws Exception {
		return tradeRecordDao.saveOrUpdate(dto);
	}

	@Override
	public TradeRecord findById(String id) {
		return tradeRecordDao.find(id);
	}

	@Override
	public TradeRecord findBySwiftNumber(String swiftNumber) {
		return tradeRecordDao.findBySwiftNumber(swiftNumber);
	}

	@Override
	public List<TradeRecord> getList(String account_id, String order_parent_type, String order_type, int pageNo,
			int pageSize) {
		return tradeRecordDao.getList(account_id,order_parent_type,order_type,pageNo,pageSize);
	}
	
	@Override
	public List<TradeRecord> getAllByDate(String account_id, String order_parent_type, String order_type,String date, int pageNo,
			int pageSize,boolean isToDay) {
		return tradeRecordDao.getAllByDate(account_id,order_parent_type,order_type,date,pageNo,pageSize,isToDay);
	}

	@Override
	public TradeRecord findByOrderNo(String orderNo) {
		return tradeRecordDao.findByOrderNo(orderNo);
	}

	@Override
	public List<TradeRecord> getList2(String account_id, String order_parent_type, String order_type, String date,
			int pageNo, int pageSize) {
		return tradeRecordDao.getList2(account_id, order_parent_type, order_type, date, pageNo, pageSize);
	}

	@Override
	public TradeRecord findByTradeSno(String tradeSno) {
		return tradeRecordDao.findByTradeSno(tradeSno);
	}

	@Override
	public void updateAndAddAbrcord(String tradeSno,AbnormalOrder abnormalOrder) throws Exception {
		//逻辑删除交易记录表
		tradeRecordDao.deleteByTranSno(tradeSno);
		//增加一条新的异常记录
		AbnormalOrder abnormalOrder2 = abnormalOrderDao.findByTranSno(tradeSno);
		if(null==abnormalOrder2){
			abnormalOrderDao.saveOrUpdate(abnormalOrder);
		}
	}

	@Override
	public Double getComsumDiscount(DTOSearchGetComsumDiscount dto) {
		return tradeRecordDao.getComsumDiscount(getComsumDisMap(dto));
	}
	
	private String getComsumDisMap(DTOSearchGetComsumDiscount dto){
		String sqlCondition = "";
		
		if(null!=dto){
			if(StringUtil.isNotEmpty(dto.getAccountId())){
				sqlCondition += " and acc_id ='"+dto.getAccountId()+"'";
			}
			
			if(StringUtil.isNotEmpty(dto.getMrchIds())){
				sqlCondition += " and mrch_id in("+StringUtil.getChangeString(dto.getMrchIds())+")";
			}
			
			if(StringUtil.isNotEmpty(dto.getOrderTypes())){
				sqlCondition += " and order_type in("+StringUtil.getChangeString(dto.getOrderTypes())+")";
			}
			
			if(StringUtil.isNotEmpty(dto.getTranBeginTime())&&StringUtil.isNotEmpty(dto.getTranEndTime())){
				sqlCondition += " and trade_end_time >= '"+dto.getTranBeginTime()+"' and trade_end_time<='"+dto.getTranEndTime()+"' ";
			}
		}
		return sqlCondition;
	}

	@Override
	public List<TradeRecord> findTradeRecordList(String account_id, String cardNos, String order_parent_type,
			String order_type, String date, int pageNo, int pageSize, boolean isToDay) {
		return this.tradeRecordDao.findTradeRecordList(account_id, cardNos, order_parent_type, order_type, date, pageNo, pageSize, isToDay);
	}

}
